#!/bin/sh

env="dev"
if [ ! -z "$GITONOMY_ENV" ]; then
    env="$GITONOMY_ENV"
fi

# Check the context information
if [ -z "$GITONOMY_USER" -o -z "$GITONOMY_PROJECT" ]; then
    echo >&2 "No environment variables GITONOMY_USER and/or GITONOMY_PROJECT"
    exit 1
fi

# Gitonomy console
file="`readlink -f "$0"`"
dir="`dirname "$file"`"
command="php $dir/../../console"

project_notify()
{
    BEFORE="$(git rev-parse $1)"
    AFTER="$(git rev-parse $2)"
    REFERENCE="$3"

    $command gitonomy:project-notify-push "$GITONOMY_PROJECT" "$GITONOMY_USER" "$REFERENCE" "$BEFORE" "$AFTER" "--env=$env"
}

while read before after reference
do
    project_notify $before $after $reference
done
